import { Injectable } from '@angular/core';
import { Subject } from "rxjs/Subject";
import { Observable } from "rxjs/Observable";

@Injectable()
export class ResizingService {
  subject: Subject<any> = new Subject();
  source;

  tick() {
    this.subject.next({});
  }
  constructor() {
    let throttle = function (type, name, obj = window) {
      // console.debug("Throttle: ", this);
      let running = false;
      let func = function () {
        if (running) { return; }
        running = true;
        requestAnimationFrame(function () {
          obj.dispatchEvent(new CustomEvent(name));
          running = false;
        });
      };
      obj.addEventListener(type, func);
    };

    /* init - you can init any event */
    throttle("resize", "optimizedResize");
    
    // this.source = Observable.fromEvent(window, "optimizedResize");
    this.source = Observable.fromEvent(window, "resize");
    // this.source = Observable.fromEvent(window, "optimizedResize").throttleTime(100);
    // this.source.subscribe(e => console.debug("Resizing: ", e));
  }

}